#pragma once
#include <iostream>
#include <pthread.h>
#include <string>
#include <functional>
#include <sys/syscall.h>
#include <unistd.h>

#define get_lwp_id() syscall(SYS_gettid)

const std::string initname = "None-name";
using func_t = std::function<void()>;

class Thread
{
private:
    static void *Routine(void *args)
    {
        Thread *th = static_cast<Thread *>(args);
        th->_func;
        th->_isrunning = true;
        th->_lwp = get_lwp_id();
        pthread_exit((void *)0);
    }

public:
    Thread(func_t func, const std::string &name)
        : _func(func), _name(name), _isrunning(false)
    {
        std::cout << "线程初始化成功" << std::endl;
    }
    void Start()
    {
        int n = pthread_create(&_tid, nullptr, Routine, this);
        if (n == 0)
            std::cout << "启动线程：" << _name.c_str() << "成功" << std::endl;
    }
    void Join()
    {
        if (!_isrunning)
            return;
        int n = pthread_join(_tid, nullptr);
        if(n==0)
            std::cout<<"线程等待成功"<<std::endl;
    }
    ~Thread() {}

private:
    pthread_t _tid;
    std::string _name;
    pid_t _lwp;
    func_t _func;
    bool _isrunning;
};